﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//
using System.Security.Cryptography;
using System.IO;

namespace DLLTest.OpertionClass
{
    class EncDecryOpertion
    {
        #region 其他加密方式
        /*摘自http://www.2cto.com/kf/201109/103450.html*/
        string EncryptKey = "fanxuanxuan";//定义密钥
        /// <summary>
        /// 字符串加密      
        /// </summary>
        /// <param name="str">要加密的字符串</param>
        /// <returns></returns>
        public string EncryptStr(string str)
        {
            DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//实例化加密解密对象
            byte[] key = Encoding.Unicode.GetBytes(EncryptKey);//定义字节数组、用来存储密钥
            byte[] data = Encoding.Unicode.GetBytes(str);//定义字节数组、用来存储要解密的字符串
            MemoryStream mstream = new MemoryStream();//实例化内存流对象
            //使用内存流实例化加密流对象
            CryptoStream cstream = new CryptoStream(mstream, desc.CreateEncryptor(key, key), CryptoStreamMode.Write);
            cstream.Write(data, 0, data.Length);//想加密流中写入数据
            cstream.FlushFinalBlock();//释放加密流
            return Convert.ToBase64String(mstream.ToArray());//返回加密后的字符串
        }
        /// <summary>
        /// 字符串解密
        /// </summary>
        /// <param name="str">要解密的字符串</param>
        /// <returns></returns>
        public string Decrypt(string str)
        {
            DESCryptoServiceProvider desc = new DESCryptoServiceProvider();//实例化加密、解密对象
            byte[] key = Encoding.Unicode.GetBytes(EncryptKey);//定义字节数组用来存储密钥
            byte[] data = Convert.FromBase64String(str);//定义字节数组用来存储密钥            
            MemoryStream mstream = new MemoryStream();//实例化内存流对象
            //使用内存流实例化解密对象
            CryptoStream cstream = new CryptoStream(mstream, desc.CreateDecryptor(key, key), CryptoStreamMode.Write);
            cstream.Write(data, 0, data.Length);//向解密流中写入数据
            cstream.FlushFinalBlock();//释放解密流
            return Encoding.Unicode.GetString(mstream.ToArray());
        }
        /* 摘自http://www.2cto.com/kf/201109/103450.html*/
        #endregion
        #region DES加密字符串
        /*摘自http://www.cnblogs.com/wuhuisheng/archive/2012/08/20/2647202.html*/
        //默认密钥向量
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

        /// <summary>
        /// DES加密字符串
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密钥,要求为8位</param>
        /// <returns>加密成功返回加密后的字符串，失败返回源串 </returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));//转换为字节
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();//实例化数据加密标准
                MemoryStream mStream = new MemoryStream();//实例化内存流
                //将数据流链接到加密转换的流
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns>解密成功返回解密后的字符串，失败返源串</returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
        /*摘自http://www.cnblogs.com/wuhuisheng/archive/2012/08/20/2647202.html*/
        #endregion
        #region mCSP加密
        /*摘自http://blog.csdn.net/sev_ran/article/details/7722616
         需引用如下命名空间：
         * System.Security.Cryptography;
         * System.IO;
         */       
        /// <summary>
        /// 字符串加密组件
        /// </summary>
        public class Encrypt
        {
            #region "定义加密字串变量"
            private SymmetricAlgorithm mCSP;  //声明对称算法变量
            private const string CIV = "Mi9l/+7Zujhy12se6Yjy111A";  //初始化向量
            private const string CKEY = "jkHuIy9D/9i="; //密钥（常量）
            #endregion

            /// <summary>
            /// 实例化
            /// </summary>
            public Encrypt()
            {
                mCSP = new DESCryptoServiceProvider();  //定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象,此类是SymmetricAlgorithm的派生类
            }

            /// <summary>
            /// 加密字符串
            /// </summary>
            /// <param name="Value">需加密的字符串</param>
            /// <returns></returns>
            public string EncryptString(string Value)
            {
                ICryptoTransform ct; //定义基本的加密转换运算
                MemoryStream ms; //定义内存流
                CryptoStream cs; //定义将内存流链接到加密转换的流
                byte[] byt;
                //CreateEncryptor创建(对称数据)加密对象
                ct = mCSP.CreateEncryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV)); //用指定的密钥和初始化向量创建对称数据加密标准
                byt = Encoding.UTF8.GetBytes(Value); //将Value字符转换为UTF-8编码的字节序列
                ms = new MemoryStream(); //创建内存流
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); //将内存流链接到加密转换的流
                cs.Write(byt, 0, byt.Length); //写入内存流
                cs.FlushFinalBlock(); //将缓冲区中的数据写入内存流，并清除缓冲区
                cs.Close(); //释放内存流
                return Convert.ToBase64String(ms.ToArray()); //将内存流转写入字节数组并转换为string字符
            }

            /// <summary>
            /// 解密字符串
            /// </summary>
            /// <param name="Value">要解密的字符串</param>
            /// <returns>string</returns>
            public string DecryptString(string Value)
            {
                ICryptoTransform ct; //定义基本的加密转换运算
                MemoryStream ms; //定义内存流
                CryptoStream cs; //定义将数据流链接到加密转换的流
                byte[] byt;
                ct = mCSP.CreateDecryptor(Convert.FromBase64String(CKEY), Convert.FromBase64String(CIV)); //用指定的密钥和初始化向量创建对称数据解密标准
                byt = Convert.FromBase64String(Value); //将Value(Base 64)字符转换成字节数组
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Encoding.UTF8.GetString(ms.ToArray()); //将字节数组中的所有字符解码为一个字符串
            }
        }
        /*摘自http://blog.csdn.net/sev_ran/article/details/7722616*/
        #endregion       
        #region MD5加密
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="realMsg">需加密的明文</param>
        /// <param name="key">加密标准算法的机密密钥</param>
        /// <param name="iv">对称算法的初始化向量</param>
        /// <returns>加密后密码的16进制表示</returns>
        public string MD5Encrypt(string realMsg, string key, string iv)
        {
            string sKey = key.PadLeft(8, '0');
            string sIV = iv.PadLeft(8, '0');
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(realMsg);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(sIV.Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            try
            {
                cs.FlushFinalBlock();
            }
            catch //(Exception err)
            {
                return realMsg;
            }
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();
        }

        /// <summary>
        /// MD5解密,对MD5Encrypt的加密内容进行解密
        /// </summary>
        /// <param name="encryptedMsg">已经加密的密码密文</param>
        /// <param name="key">加密标准算法的机密密钥</param>
        /// <param name="iv">对称算法的初始化向量</param>
        /// <returns></returns>
        public string MD5Decrypt(string encryptedMsg, string key, string iv)
        {
            string sKey = key.PadLeft(8, '0');
            string sVI = iv.PadLeft(8, '0');
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = new byte[encryptedMsg.Length / 2];
            for (int x = 0; x < encryptedMsg.Length / 2; x++)
            {
                int i = Convert.ToInt32(encryptedMsg.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey.Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(sVI.Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            try
            {
                cs.FlushFinalBlock();
            }
            catch//(Exception err)
            {
                return encryptedMsg;
            }
            StringBuilder ret = new StringBuilder();
            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }
        #endregion
    }
}
